package cc.customer.web.myCustomer;

import cc.customer.common.domain.QueryRequest;
import cc.customer.common.domain.ResponseBo;
import cc.customer.common.utils.*;
import cc.customer.myCustomer.damain.MyCustomer;
import cc.customer.myCustomer.services.PerSonServices;
import cc.customer.system.domain.MyUser;
import cc.customer.system.service.UserService;
import cc.customer.web.sys.controller.base.BaseController;
import cc.customer.web.util.BigEmpReadExcel;
import cc.customer.web.util.PersonExcel;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping(value="PerSonCustomer")
public class PerSonCustomerController extends BaseController {

    private Logger logger = LoggerFactory.getLogger(PerSonCustomerController.class);

    @Autowired
    private PerSonServices perSonServices;

    @Autowired
    private UserService userService;

    @RequestMapping(value="loadAddCustomer")
    public String loadAddCustomer(){return "customer/myCustomer/myCustomer_add";}

    /**新增我的客户*/
    @RequestMapping(value="addMyCustomer")
    @ResponseBody
    public ResponseBo addMyCustomer(MyCustomer customer){
        try {
            MyUser user = this.getCurrentUser();
            String userid = user.getUserUuid();
            String deptid = user.getDeptId()+"";
            String uuid = Utils.getUUID();
            customer.setMyCusCreatetime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            customer.setMyCusCreator(userid);
            customer.setMyCusUuid(uuid);
            customer.setMyCusReceiver(userid);
            customer.setMyCusStage("1");
            customer.setMyCusIsdelete("0");
            customer.setMyCusDept(deptid);
            customer.setMuRecent(user.getNickname()+"录入了新的客户");
            customer.setMyRecentTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
            return perSonServices.addMyCustomer(customer);
        }catch (Exception e){
            e.printStackTrace();
            return ResponseBo.error("新增失败");
        }
    }
    /**下载模板**/
    @RequestMapping(value = "downPersonModul")
    @ResponseBody
    public  ResponseBo downPersonModul(){
        try {
            MyCustomer excel = new MyCustomer();
            excel.setMyCusName("张三");
            excel.setMyCusSex("男");
            excel.setMyCusMobile("15253130120");
            excel.setMyCusQq("1114701778");
            excel.setMyCusEmail("12345468@qq.com");
            excel.setMyCusWechat("15253130120");
            excel.setMyCusCity("济南地区");
            List<MyCustomer> list = new ArrayList<>();
            list.add(excel);
            return FileUtils.createExcelByPOIKit("我的客户批量导入表格模板", list, MyCustomer.class);
        } catch (Exception e) {
            return ResponseBo.error("导出Excel失败，请联系网站管理员！");
        }
    }
    /**校对个人资源资源的导入**/
    @RequestMapping(value = "/PersonValiableExport",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo valiableBigEmpExport(String expUrl){
        //读取列术，查看内容，，自动义验证规则
        return PersonExcel.PersonValiableExport(expUrl);
    }
    /**批量增加个人客户的信息**/
    @RequestMapping(value = "batchPersonInsert",method = RequestMethod.POST)
    @ResponseBody
    public ResponseBo batchPersonInsert(String expUrl){
        try {
            MyUser user = this.getCurrentUser();
            List<MyCustomer> mylist = PersonExcel.createExcelExport(expUrl,user);
            if (!mylist.isEmpty()){
                return perSonServices.batchAddPersonCus(mylist);
            }
        }catch (Exception e){
            e.printStackTrace();
            logger.error("导入个人资源失败:"+e.getMessage());
        }
        return ResponseBo.error("获取数据失败");
    }


    /**客户重复 申请共享*/
    @RequestMapping(value="shenqingShare")
    @ResponseBody
    public ResponseBo shenqingShare(String uuid,String reid,String content,String name){
        MyUser user = this.getCurrentUser();
        return perSonServices.shenqingShare(user,uuid,reid,content,name);
    }
    /**加载客户详情*/
    @RequestMapping(value="getMyCustomerDetail")
    @ResponseBody
    public ResponseBo selectMyDetail(MyCustomer customer){
        String uuid = customer.getMyCusId()+"";
        if(StringUtils.isEmpty(uuid)){
            return ResponseBo.error("数据加载失败");
        }
        MyUser user = this.getCurrentUser();
        return perSonServices.selectMyCusDetail(customer,user);
    }
    /**加载我的客户详情的标签*/
    @RequestMapping(value="selectMyCuatomerLabel")
    @ResponseBody
    public ResponseBo selectMyCuatomerLabel(String ids){
        return perSonServices.selectMyCuatomerLabel(ids);
    }

    /**修改个人客户详情*/
    @RequestMapping(value="updateCusDetail")
    @ResponseBody
    public ResponseBo updateCusDetail(MyCustomer customer){
        String uuid = customer.getMyCusId()+"";
        if(StringUtils.isEmpty(uuid)){
            return ResponseBo.error("数据加载失败");
        }
        MyUser myUser = this.getCurrentUser();
        String username= myUser.getNickname();
        customer.setMuRecent(username+ Constant.GENZONG_RECORD_ZILAO);
        customer.setMyRecentTime(DateUtil.getCurrentDate("yyyy-MM-dd HH:mm:ss"));
        return perSonServices.updateMyCusDetail(customer,myUser);
    }
    /**
     * 新增客户验证手机号
     */
    @RequestMapping(value = "/checkUserMobile")
    @ResponseBody
    public Boolean checkUserMobile(String myCusMobile){
        Boolean ble = FebsUtil.isPhoneNo(myCusMobile);
        if(!ble){
            return false;
        }else{
            return true;
        }
    }
    /**查询个人录入的 客户列表**/
    @RequestMapping(value="selectCusList")
    @ResponseBody
    public Map<String, Object> selectCusList(MyCustomer customer, QueryRequest request){
        MyUser user = this.getCurrentUser();
        List<String> list = user.getAllAuthority();
        if (!list.contains(Constant.AMDIN_AUTORITY)){
            String usersids = customer.getMyCusCreator();
            if (StringUtils.isEmpty(usersids)){
                List<String> listUswers = new ArrayList<>();
                List<MyUser> listAllUser = userService.selectUserByDeptid(user);
                listAllUser.forEach(usesr->{
                    listUswers.add(usesr.getUserUuid());
                });
                String jonUser = StringUtils.join(listUswers,",");
                customer.setMyCusCreator(jonUser);
            }
        }
        return super.selectByPageNumSize(request, () -> this.perSonServices.selectMyCustomer(customer,user));
    }

}
